package com.gp.learn.ssm.base.system.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.gp.learn.ssm.base.core.mybaits.base.service.impl.BaseServiceImpl;
import com.gp.learn.ssm.base.core.security.model.OpenAuthority;
import com.gp.learn.ssm.base.system.entity.BaseAuthority;
import com.gp.learn.ssm.base.system.entity.BaseRole;
import com.gp.learn.ssm.base.system.mapper.BaseAuthorityMapper;
import com.gp.learn.ssm.base.system.service.BaseAuthorityService;
import com.gp.learn.ssm.base.system.service.BaseAuthorityUserService;
import com.gp.learn.ssm.base.system.service.BaseRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * 系统权限-菜单权限、操作权限、API权限 服务实现类
 *
 * @author gp
 * @since 2020-08-07
 */
@Service
public class BaseAuthorityServiceImpl extends BaseServiceImpl<BaseAuthorityMapper, BaseAuthority> implements BaseAuthorityService {

    @Autowired
    private BaseRoleUserService baseRoleUserService;

    @Autowired
    private BaseAuthorityUserService baseAuthorityUserService;

    /**
     * 获取所有可用权限
     *
     * @param type = null 查询全部  type = 1 获取菜单和操作 type = 2 获取API
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<OpenAuthority> findAuthorityByType(String type) {
        Map map = Maps.newHashMap();
        map.put("type", type);
        map.put("status", 1);
        return baseMapper.selectAuthorityAll(map);
    }

    /**
     * 获取角色已授权权限
     *
     * @param roleId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<OpenAuthority> findAuthorityByRole(Long roleId) {
        return baseMapper.selectAuthorityByRole(roleId);
    }


    @Override
    public List<OpenAuthority> findAuthorityByUser(Long userId, boolean isSuper) {
        if (isSuper) {
            // 超级管理员返回所有
            return findAuthorityByType("1");
        }
        List<OpenAuthority> authorities = Lists.newArrayList();
        List<BaseRole> rolesList = baseRoleUserService.selectRoleListByUser(userId);
        if (rolesList != null) {
            for (BaseRole role : rolesList) {
                // 加入角色已授权
                List<OpenAuthority> roleGrantedAuthority = findAuthorityByRole(role.getRoleId());
                if (roleGrantedAuthority != null && roleGrantedAuthority.size() > 0) {
                    authorities.addAll(roleGrantedAuthority);
                }
            }
        }
        // 加入用户特殊授权
        List<OpenAuthority> userGrantedAuthority = baseAuthorityUserService.selectAuthorityByUser(userId);
        if (userGrantedAuthority != null && userGrantedAuthority.size() > 0) {
            authorities.addAll(userGrantedAuthority);
        }
        // 权限去重
        HashSet h = new HashSet(authorities);
        authorities.clear();
        authorities.addAll(h);
        return authorities;
    }
}
